package com.demo.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.common.QueryPageParam;
import com.demo.common.Result;
import com.demo.entity.Record;
import com.demo.service.RecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author demo
 * @since 2024-07-09
 */
@RestController
@RequestMapping("/record")
public class RecordController {
    @Autowired
    private RecordService recordService;
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody Record record){
        return recordService.save(record)?Result.suc():Result.fail();
    }
    //更新
    @PostMapping("/update")
    public Result update(@RequestBody Record record){
        return recordService.updateById(record)?Result.suc():Result.fail();
    }
    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return recordService.removeById(id)?Result.suc():Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String name = (String)param.get("name");
        String goodstype = (String)param.get("goodstype");
        String kind = (String)param.get("kind");

        Page<Record> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        QueryWrapper<Record> QueryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(name) && !"null".equals(name) && !name.isEmpty()){
            QueryWrapper.like("b.userName",name);
        }
        if(StringUtils.isNotBlank(goodstype) && !"null".equals(goodstype)){
            //lambdaQueryWrapper.eq(Record::getGoodstype,goodstype);
        }
        if(StringUtils.isNotBlank(kind) && !"null".equals(kind)){
            //lambdaQueryWrapper.eq(Record::getKind,kind);
        }
        IPage result = recordService.pageCC(page,QueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }
    @GetMapping("/list")
    public Result list(){
        List list = recordService.list();
        return Result.suc(list);
    }
}
